### Dev Stage

FROM node:17 AS development

WORKDIR /usr/src/app

RUN npm config set registry https://registry.npmmirror.com && \
  npm install -g pnpm

COPY pnpm-lock.yaml ./
RUN npm_config_bcrypt_binary_host_mirror=https://github.com.cnpmjs.org pnpm fetch

COPY --chown=node:node . .

RUN pnpm install --offline
RUN pnpm run build

USER node
CMD ["pnpm", "run", "start:dev"]

### Prod Stage

FROM node:17 AS production

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

RUN npm config set registry https://registry.npmmirror.com && \
  npm install -g pnpm

COPY pnpm-lock.yaml ./
RUN npm_config_bcrypt_binary_host_mirror=https://github.com.cnpmjs.org pnpm fetch --prod

COPY --chown=node:node . .

RUN pnpm install --offline --prod
COPY --from=development /usr/src/app/dist ./dist

USER node
CMD ["pnpm", "run", "start:prod"]
